package com.ljf.weather.job;

import com.ljf.weather.domain.WeatherResponse;
import com.ljf.weather.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WeatherJob {

    @Autowired
    WeatherService weatherService;

    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron="0/30 * * * * *")
    public void job(){
        List<String> cityList = new ArrayList<>();
        cityList.add("101010200");
        cityList.add("101020200");
        cityList.add("101030200");
        cityList.add("101040200");

        for (int i = 0; i < cityList.size(); i++) {
            System.out.println(new Date() +"正在缓存cityId为"+cityList.get(i)+"的数据");
            WeatherResponse weatherResponse = weatherService.getWeather(cityList.get(i));
            redisTemplate.opsForHash().put("WeatherTable",cityList.get(i),weatherResponse);
        }
    }
}

